<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input v-model="params.name" clearable placeholder="请输入内容" @clear="getList" class="search-main" />
      <el-button type="primary" @click="getList">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="addEmployee">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="员工姓名" width="180" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" width="120" prop="phonenumber" />
        <el-table-column label="角色" width="120" prop="roleName" />
        <el-table-column label="状态">
          <template #default="scope">
            {{ formatStatus(scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="220">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
            <el-button size="mini" type="text" @click="cz">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="box">
      <el-pagination :current-page="params.page" :page-sizes="[2, 5, 10]" :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
    <!-- 添加员工或编辑员工 -->

    <el-dialog :title="addForm.id ? '编辑员工' : '添加员工'" :visible="dialogVisible" width="480px" @close="closeDialog"
      @open="openDialog">

      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>

          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>

          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>

          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option label="超级管理员" :value="1" />
              <el-option label="基础数据管理" :value="2" />
              <el-option label="行车管理" :value="3" />
              <el-option label="员工及系统管理" :value="4" />
              <el-option label="黑马测试" :value="5" />
            </el-select>
          </el-form-item>

          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>

        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>
      </template>
    </el-dialog>


  </div>
</template>

<script>
import { getEmployeeListAPI, delEmployeeAPI, addEmployeeAPI, editEmployeeAPI } from '@/api/employee'
// import { getRoleListAPI } from '@/api/system' //

export default {
  name: 'Employee',
  data() {
    return {

      dialogVisible: false, // 控制弹框打开关闭 默认关闭
      roleList: [],
      addForm: {
        name: '',
        phonenumber: '',
        roleId: '',
        status: '',
        userName: ''
      },

      employeeList: [],
      params: {
        page: 1,
        pageSize: 5,
        name: '' // 员工姓名
      },

      addFormRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请分配角色', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择员工状态', trigger: 'blur' }
        ]
      }
    }
  },

  mounted() {
    this.getList()
  },

  methods: {

    //重置密码
    cz() {
      this.$confirm('确定将密码重置为"123456"?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '重置成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消重置'
        })
      })
    },


    //编辑
    edit(row) {
      console.log(row)
      this.dialogVisible = true //打开弹窗

      //数据回显
      this.addForm.name = row.name
      this.addForm.phonenumber = row.phonenumber
      this.addForm.roleId = row.roleId
      this.addForm.status = row.status
      this.addForm.userName = row.userName
      this.addForm.roleName = row.roleName

      this.addForm.id = row.id
    },

    //状态
    formatStatus(status) {
      return status === 1 ? '启用' : '禁用'
    },

    //添加或编辑
    confirmAdd() {
      // 添加/编辑 => 都要做校验
      this.$refs.addForm.validate((ok) => {
        if (ok) {
          // console.log('可以请求数据了')
          if (this.addForm.id) {
            // console.log('编辑')
            editEmployeeAPI(this.addForm).then(() => {
              this.$message.success('编辑成功')
              this.getList()
              // 关闭弹窗
              this.dialogVisible = false

              //一定要记得清空数据 删除id字段
              this.addForm = {
                name: '',
                phonenumber: '',
                roleId: '',
                status: '',
                userName: ''

              }
            })

          } else {
            addEmployeeAPI(this.addForm).then(() => {
              // console.log('添加')
              this.$message.success('添加成功')
              this.getList()
              this.dialogVisible = false

              this.addForm = {
                name: '',
                phonenumber: '',
                roleId: '',
                status: '',
                userName: ''
              }
            })
          }
        }
      })
    },
    //获取渲染角色列表
    async openDialog() {
      const res = await getRoleListAPI()
      this.roleList = res.data
    },

    // 关闭弹窗
    closeDialog() {
      this.dialogVisible = false

      this.addForm = {
        name: '',
        phonenumber: '',
        roleId: '',
        status: '',
        userName: ''
      }

    },

    // 打开弹框
    addEmployee() {
      this.dialogVisible = true
    },

    //获取
    async getList() {
      const res = await getEmployeeListAPI(this.params)
      this.employeeList = res.data.rows
      this.total = res.data.total
    },

    //删除
    del(id) {
      this.$confirm('你确认要删了我么?').then(() => {
        // 二次确认 请求 提示 重新获取
        delEmployeeAPI(id).then(() => {
          // 需要在本页最后一条数据时 回到上一页
          // 此时后端已经删了这个数据了 前端数据还没更新 还有刚刚这一条
          if (this.employeeList.length === 1) this.params.page--

          this.$message.success('删除成功')
          this.getList()
          // this.form = []
        }).catch(() => {
        })
      })
    },



    // async getList() {
    //   const res = await getEmployeeListAPI(this.params)
    //   this.employeeList = res.data.rows
    //   this.total = res.data.total
    // },

    // 分页
    handleCurrentChange(page) {
      console.log('页码变了', page)
      this.params.page = page  // 更新页码
      this.getList()
    },
    handleSizeChange(pageSize) {
      console.log('每页条数变了', pageSize)
      this.params.pageSize = pageSize  //更新每页条数
      // this.params.page = 1
      this.getList()
    },
  }
}
</script>

<style lang="scss" scoped>
.box {
  float: right;
  // flex 弹性容器布局(快速实现子元素横向布局)
  // display: flex;
  // 默认是从左往右  从右往左
  // justify-content: flex-end;

  margin-top: 10px;
  margin-right: 20px;
}

.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>